package wads.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Properties;
import java.util.Vector;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import wads.com.AutoDocumentTransaction;
import wads.com.WADTools;

public class ShowDocumentServlet extends HttpServlet {
  private static final long serialVersionUID = 8477650078517501562L;
  
  private static final String CONTENT_TYPE = WADTools.SERVLET_CONTENT_TYPE;
  /*Initialize global variables*/
  private String powerUser;
  private String transactionID;
  private String app_Path;
  
  private Vector history = new Vector();
  private Properties prop = new Properties();
  private File docFile;
  
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
  }
  
  /*Process the HTTP Get request*/
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
    transactionID = request.getParameter("ID");
    powerUser = request.getParameter("user");
    app_Path = WADTools.APP_PATH;
    
    if(powerUser.equals("SuccessDocument"))
      docFile = new File(app_Path+"res/docs/SuccessDocument/"+"trans_"+transactionID+".info");
    else
      docFile = new File(app_Path+"res/docs/InProgressAutoDoc/"+powerUser+"trans_"+transactionID+".info");
    
    prop.load(new FileInputStream(app_Path+"res/wad_app.properties"));
    
    //add
    try{
      ObjectInputStream ois = new ObjectInputStream(new FileInputStream(docFile));
      AutoDocumentTransaction adt = (AutoDocumentTransaction)ois.readObject();
      String template_ID_form = adt.getTemplate_ID();
      
      ChangeHtmlToText test = new ChangeHtmlToText(template_ID_form, adt, powerUser);
      Vector vector = test.getVector();
      String_Write_or_Print swr = new String_Write_or_Print();
      String text;
      
      ServletOutputStream sos = response.getOutputStream();
      
      byte[] buf = new byte[1024];
      history = adt.getTransaction_History();
      String Comment_powerUser_title = " ";
      
      for(int i=0;i<vector.size();i++){
        swr = (String_Write_or_Print)vector.elementAt(i);
        text = swr.text;
        System.out.println("This is <text> value: "+text);
        if(swr.write_or_print == false){//write
          //filter to don't show comment text area and radio button
          if(this.history.size() != 0){
            Comment_powerUser_title = "<br><div align=\"left\">Comments:</div><br>"+
            "<table border=\"1\"><tr bgcolor=\"#CCFFCC\"><td width=\"50\"><div align=\"center\">Number</div></td>"+
            "<td width=\"200\"><div align=\"center\">Date time Approve</div></td>"+
            "<td width=\"200\"><div align=\"center\">Approval Name</div></td><td width=\"300\"><div align=\"center\">Comment</div></td></tr>";
          }
          
          if(text.equals(WADTools.dupHTMLSpace("<br>", 0, 12)+
              "<input type=\"radio\" name=\"accept\" value=\"1\">อนุมัติ"+
              "<input type=\"radio\" name=\"accept\" value=\"0\">ไม่อนุมัติ<br>ข้อคิดเห็น:<br><textarea name=\"comment\" cols=\"70\" rows=\"8\"></textarea>"+
              WADTools.dupHTMLSpace("<br><br>", 9, 0)+
              "<input type=\"submit\" name=\"okbutton\" value=\"ตกลง\"><input type=\"hidden\" name=\"currentUser\" value=\""+
              powerUser+"\"><input type=\"hidden\" name=\"transaction_ID\" value=\""+transactionID+"\">"+
              WADTools.dupHTMLSpace("</form>", 0, 5)+"ข้อคิดเห็นจาก:<br>"+
              "<table border=\"1\"><tr bgcolor=\"#CCFFCC\"><td width=\"50\"><div align=\"center\">ลำดับที่</div></td>"+
              "<td width=\"200\"><div align=\"center\">วัน เวลาที่อนุมัติ</div></td><td width=\"200\"><div align=\"center\">ชื่อผู้อนุมัติ</div></td>"+
              "<td width=\"300\"><div align=\"center\">ข้อคิดเห็น</div></td></tr>")){
            
            buf = Comment_powerUser_title.getBytes();
            sos.write(buf, 0, Comment_powerUser_title.length());
            //sos.println(Comment_powerUser_title);
          }
          else if(text.equals(WADTools.dupHTMLSpace("<br>", 0, 12)+
              "<input type=\"radio\" name=\"accept\" value=\"1\">อนุมัติ"+
              "<input type=\"radio\" name=\"accept\" value=\"0\">ไม่อนุมัติ<br>ข้อคิดเห็น:<br>"+
              "<textarea name=\"comment\" cols=\"70\" rows=\"8\"></textarea>"+
              WADTools.dupHTMLSpace("<br><br>", 9, 0)+
              "<input type=\"submit\" name=\"okbutton\" value=\"ตกลง\"><input type=\"hidden\" name=\"currentUser\" value=\""+
              powerUser+"\"><input type=\"hidden\" name=\"transaction_ID\" value=\""+
              transactionID+WADTools.dupHTMLSpace("\"></form>", 0, 5)+" ")){
            
            String temp = "<br>";
            buf = temp.getBytes();
            sos.write(buf,0,temp.length());
          }
          else{
            buf = text.getBytes();
            sos.write(buf, 0, text.length());
          }
        }
        else
          sos.println(text);
      }
      
      sos.close();
      ois.close();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  
  /*Clean up resource*/
  public void destroy() {
  }
}